package com.yangyouqi.posthorse.web.controller;

import com.yangyouqi.posthorse.core.domain.Customer;
import com.yangyouqi.posthorse.core.service.CustomerService;
import com.yangyouqi.posthorse.web.from.LoginFrom;
import com.yangyouqi.posthorse.web.from.RegisterFrom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

import static com.yangyouqi.posthorse.web.controller.OrderController.getsession;

/**
 * @author 杨友奇
 * @Time 2018/6/27
 * 公开的控制器，所有不需要身份认证的方法都会放到这个控制器中
 */

@Controller
@RequestMapping("/publiz")
public class publizController {

    @Autowired
    public CustomerService customerService;

    /**
     * 主页面,没有进行登陆便以游客身份进行浏览
     *
     * @return
     */
    @GetMapping("/index")
    public String index() {
        return "publiz/index";
    }

    //进入注册页面
    @GetMapping("/register")
    public String register() {
        return "publiz/register";
    }

    //执行注册操作
    @PostMapping("register")
    public String register(@Validated RegisterFrom registerFrom, BindingResult result1,
                           Model model) {
        //进行判断用户输入是否非法
        if (result1.hasErrors()) {
            List<ObjectError> list = result1.getAllErrors();
            for (ObjectError error : list) {
                System.out.println(error.getDefaultMessage());
                model.addAttribute("errorMsg", error.getDefaultMessage());
                return "publiz/register";
            }
        }
        //进行判断数据库中是否存在该用户
        if (customerService.existsusername(registerFrom.getUsername())) {
            model.addAttribute("errorMsg", "用户已存在");
            return "publiz/register";
        } else {
            //进行注册
            Customer customer = new Customer(registerFrom.getUsername(), registerFrom.getNickname(),
                    registerFrom.getPassword(), registerFrom.getMobile());

            Customer result = customerService.register(customer);
            if (result != null) {
                //注册成功,跳转到登录界面，并携带相关提示信息
                model.addAttribute("customer", customer);
                return "publiz/regsuccess";
            } else {
                //注册失败
                return "publiz/register";
            }
        }

    }

    //进入登录页面
    @GetMapping("/login")
    public String login() {
        return "publiz/login";
    }

    //执行登录操作
    @PostMapping("/login")
    public String login(@Validated LoginFrom loginFrom, BindingResult result,
                        HttpSession session, Model model) {
        //进行判断用户输入是否非法
        if (result.hasErrors()) {
            List<ObjectError> list = result.getAllErrors();
            for (ObjectError error : list) {
                model.addAttribute("Msg", error.getDefaultMessage());
                return "publiz/login";
            }
        }
        Customer customer = customerService.login(loginFrom.getUsername(), loginFrom.getPassword());
        if (customer == null) {
            //登录失败,重新跳转到登录界面，并给出相关提示
            model.addAttribute("Msg", "用户名或密码错误");
            return "publiz/login";
        } else {
            //登录成功
            //将用户信息存放到session
            session.setAttribute("customer", customer);
            //重定向到下单界面
            return "redirect:/order/index";
        }
    }

    /**
     * 进入关于我们页面
     *
     * @return
     */
    @GetMapping("/aboutUs")
    public String aboutUs() {

        return "publiz/aboutUs";
    }


}
